<?php

/**
 * 用户操作
 * @author pirate
 */
class Edm_Db_Users extends Zend_Db_Table
{
    protected $_name = 'users';
    protected $_primary = 'user_id';
    protected $_db;

    public function init()
    {
        $this->_db = $this->getAdapter();
        parent::init();
    }

    /*
     * 统计用户总数
     * @return int 用户数量
     */
    public function userCount()
    {
        return $this->fetchAll()->count();
    }

    /*
     * 用户登录
     * @param array $data 验证字段=>值
     * @return bool
     */
    public function login($data)
    {
        $flag = false;
        $auth = Zend_Auth::getInstance();
        $adapter = new Zend_Auth_Adapter_DbTable($this->_db, $this->_name, 'user_name', 'password', 'md5(?)');
        $adapter->setIdentity($data['user_name']);
        $adapter->setCredential($data['password']);
        $res = $auth->authenticate($adapter);
        if ($res->isValid()) {
            $identity = $adapter->getResultRowObject(array('user_id', 'user_name', 'reg_time'));
            $this->Update(array('last_login' => time()), $identity->user_id);
            $auth->getStorage()->write($identity);
            $flag = true;
        }
        return $flag;
    }

    /*
     * 根据用户名更新最后登录时间
     * @param array $data 更新字段
     * @param mix $user 用户id或用户名
     * @return bool
     */
    public function Update($data, $user)
    {
        if (is_numeric($user)) {
            $where = $this->_db->quoteInto('user_id = ?', $user);
        } else {
            $where = $this->_db->quoteInto('user_name = ?', $user);
        }
        return $this->_db->update($this->_name, $data, $where);
    }

    /*
     * 用户验证
     * @param array $data 检测项=>值
     * @return bool
     */
    public function userCheck($data)
    {
        if (isset($data['user_id'])) {
            $where = $this->_db->quoteInto('user_id = ?', $data['user_id']);
        } else {
            $where = $this->_db->quoteInto('user_name = ?', $data['user_name']);
        }
        /* 用户修改密码时 检测原密码是否正确 */
        if (isset($data['password'])) {
            $where .= $this->_db->quoteInto(' AND password = ?', md5($data['password']));
        }
        $sql = "SELECT COUNT(*) FROM " . $this->_name . " WHERE " . $where;
        return $this->_db->fetchOne($sql);
    }

    /*
     * 用户注册 插入数据
     * @param array $data 检测项=>值
     * @return int insert_id
     */
    public function Insert($data)
    {
        $this->_db->insert($this->_name, $data);
        return $this->_db->lastInsertId($this->_name, $this->_primary);
    }

    /*
     * 删除用户
     * @param int $user_id 用户编号
     * @return bool
     */
    public function Delete($user_id)
    {
        $where = $this->_db->quoteInto('user_id = ?', $user_id);
        return $this->_db->delete($this->_name, $where);
    }

    /*
     * 根据user_id 或 user_name 获取用户信息
     * @param array $data 用户信息
     * @return array 用户信息
     */
    public function userInfo($data)
    {
         /* 转换时间戳 */
         if (!isset($data['cols'])) {
             $data['cols'] = array('user_id, user_name, email, FROM_UNIXTIME(reg_time) AS reg_time, FROM_UNIXTIME(last_login) AS last_login');
         } else {
             $data['cols']['reg_time'] = ((isset($data['cols']['reg_time']) && $data['cols']['reg_time'] == 'reg_time')) ? 'FROM_UNIXTIME(reg_time) AS reg_time' : $data['cols']['reg_time'];
             $data['cols']['last_login'] = ((isset($data['cols']['last_login']) && $data['cols']['last_login'] == 'last_login')) ? 'FROM_UNIXTIME(last_login) AS last_login' : $data['cols']['last_login'];
         }
        if (isset($data['user_id'])) {
            $where = $this->_db->quoteInto('user_id = ?', $data['user_id']);
        } else {
            $where = $this->_db->quoteInto('user_name = ?', $data['user_name']);
        }
        $sql = "SELECT ". join(', ', $data['cols']) ." FROM " . $this->_name . " WHERE " . $where . " LIMIT 1";
        $rows = $this->_db->fetchRow($sql);
        if (!$rows) {
            return NULL;
        }
        return $rows;
    }

    /*
     * 根据条件获取用户列表
     * @param array $data 搜索条件
     * @return array rows
     */
     public function userList($data)
     {
         $select = $this->_db->select();
         /* 转换时间戳 */
         if (!isset($data['cols'])) {
             $data['cols'] = array('user_id, user_name, email, FROM_UNIXTIME(reg_time) AS reg_time, FROM_UNIXTIME(last_login) AS last_login');
         } else {
             $data['cols']['reg_time'] = ((isset($data['cols']['reg_time']) && $data['cols']['reg_time'] == 'reg_time')) ? 'FROM_UNIXTIME(reg_time) AS reg_time' : $data['cols']['reg_time'];
             $data['cols']['last_login'] = ((isset($data['cols']['last_login']) && $data['cols']['last_login'] == 'last_login')) ? 'FROM_UNIXTIME(last_login) AS last_login' : $data['cols']['last_login'];
         }
         
         $select->from($this->_name, $data['cols']);
         /* 关键字 */
         if (!empty($data['keywords'])) {
             $select->where('user_name LIKE ?', '%'. $data['keywords'] .'%');
             $select->orWhere('email = ?', $data['keywords']);
         }
         /* 排序 */
         if (!isset($data['orderBy'])) {
             $select->order('user_id DESC');
         } else {
             $select->order($data['orderBy']);
         }
         
        $select->sqlcalcfoundrows();
        $select->limitPage($data['pageNum'], $data['pageSize']);
        $res = $this->_db->fetchAll($select);
        $count = $this->_db->fetchOne('SELECT FOUND_ROWS()');
        
        return array('list' => $res, 'count' => $count);
     }

}